package br.com.caelum.vraptor.controller;

import java.util.List;

import javax.inject.Inject;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.components.AdmSessioned;
import br.com.caelum.vraptor.dao.AdministradorDAO;
import br.com.caelum.vraptor.dao.GenericDAO;
import br.com.caelum.vraptor.entidades.Administrador;
@Controller
public class AdministradorController {
	
	@Inject
    private Result result;
    
    @Inject
    private GenericDAO<Administrador> dao;
    @Inject
    private AdministradorDAO administradorDAO;
    @Inject
    private AdmSessioned admSessioned;

    
    public void add(){
            
    }
    
    public List<Administrador> list(){
            return dao.find();
    }
    
    public void save(Administrador administrador){
            dao.save(administrador);
            result.redirectTo(this).list();
    }
    
    public Administrador edit(int id){
        return dao.find(id);
    }
    
    public void delete(int id){
    	Administrador administrador= new Administrador();
        administrador.setId(id);
        dao.delete(administrador);
        result.redirectTo(this).list();
    }
    
    public void login(String matricula, String senha){
     	Administrador adm = administradorDAO.find(matricula, senha);
         if(adm==null){
        	 result.redirectTo(IndexController.class).index();
             }
         		admSessioned.login(adm);
            	result.redirectTo(IndexController.class).home();
            	 }
     
     public void logout(){
             admSessioned.logout();
             result.redirectTo(IndexController.class).index();
     }
   

}
